#include <iostream>
#include <cstdio>
#include <signal.h>

//打印pending
void PrintPending(sigset_t &pending)
{
    std::cout << "我是一个进程(" << getpid() << "), pending: ";
    for (int signo = 31; signo > 0; signo--)
    {
        if (sigismember(&pending, signo))
        {
            std::cout << "1";
        }
        else std::cout << "0";  
    }
    std::cout << "\n-------------------------------------" << std::endl;
}

//2号新号自定义捕捉动作
void handlerSig(int sig)
{
    std::cout << "signal " << sig << "!" << std::endl;
}

int main()
{
    signal(SIGINT, handlerSig);

    // 1.屏蔽2号信号
    sigset_t block, oblock;
    sigemptyset(&block);
    sigaddset(&block, SIGINT);

    int n = sigprocmask(SIG_SETMASK, &block, &oblock);
    (void)n;

    // 2.获取pending信号集合并打印
    int cnt = 0;
    sigset_t pending;
    while (true)
    {
        int m = sigpending(&pending);
        (void)m;
        PrintPending(pending);
        cnt++;
        if (cnt == 5)
        {
            //解除对2号信号的屏蔽
            std::cout << "解除对2号信号的屏蔽" << std::endl;
            sigprocmask(SIG_SETMASK, &oblock, nullptr);
        }
        sleep(1);
    }

    return 0;
}